#include "MouseClick.hpp"

namespace RoboPioneers::Debug
{
    bool MouseClick::Execute(cv::Mat &src)
    {
        using namespace cv;
        using namespace std;
        char temp[16];

        if (src.empty())
        {
            cout << "The image is empty, please check it!" << endl;
            return -1;
        }

        setMouseCallback(WindowsName, ShowPixelXY,(void*)this); //类型转换
        namedWindow(WindowsName);
        sprintf(temp, "(%d,%d)", X, Y);
        std::cout<<X<<' '<<Y<<std::endl;
        putText(src, temp, Point(X, Y), FONT_HERSHEY_SIMPLEX, 0.5, Scalar(0, 0, 0, 255));
        imshow(WindowsName, src);
    }

    void MouseClick::ShowPixelXY(int event, int x, int y, int flag, void *parameter)
    {
        using namespace cv;
        auto object=static_cast<MouseClick*>(parameter);
        cv::Mat image = (object->Picture); // 先转换类型，再取数据
        cv::Vec3b depth;    // 初始化不可放在case内

        // 一般包含如下3个事件
        switch (event)
        {
            case cv::EVENT_MOUSEMOVE:
                //std::cout << "Please select one point:" << std::endl;
                break;
            case cv::EVENT_LBUTTONDOWN: // 鼠标左键按下

                object->X=x;
                object->Y=y;

                break;
            case cv::EVENT_LBUTTONUP: // 鼠标左键释放
                //std::cout << "buttonup" << x<<' '<<y<<std::endl;

                break;
        }
    }
}